package com.yydm.serviceedu.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yydm.commonutils.R;
import com.yydm.commonutils.ordervo.AutomobileVo;
import com.yydm.serviceedu.entity.Automobile;
import com.yydm.serviceedu.entity.AutomobileCategory;
import com.yydm.serviceedu.entity.Founder;
import com.yydm.serviceedu.service.AutomobileService;
import com.yydm.serviceedu.service.IFounderService;
import com.yydm.serviceedu.vo.AutomobileQueryFrontVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Classname AutomobileFront
 * @Description TODO
 * @Version 1.0.0
 * @Date 2022/7/11 21:21
 * @Created by liu25
 */
@RestController
@RequestMapping("/serviceauto/automobileFront")
//@CrossOrigin
public class AutomobileFront {

    @Autowired
    private AutomobileService automobileService;

    @Autowired
    private IFounderService founderService;

    //    分页查询汽车的方法
    @GetMapping("/getAutomobileFront/{current}/{limit}")
    public R getAutomobileFront(@PathVariable int current, @PathVariable int limit) {

        Page<Automobile> pageParam = new Page<Automobile>(current, limit);
        Map<String, Object> map = automobileService.getAutomobileFront(pageParam);
//        返回分页所有数据
        return R.ok().data(map);
    }

    //    根据汽车id查询汽车信息
//    根据汽车中的创始人id查询所有汽车的创始人
    @GetMapping("/automobileFounderInfo/{id}")
    public R getAutomobileFounderInfo(@PathVariable String id) {

        Automobile automobile = automobileService.getById(id);

        QueryWrapper<Founder> wrapper = new QueryWrapper<>();
        wrapper.eq("id", automobile.getFounderId());
        Founder one = founderService.getOne(wrapper);

        return R.ok().data("automobile", automobile).data("founder", one);
    }

    //    分页查询创始人的方法
    @GetMapping("/founderFrontInfo/{current}/{limit}")
    public R getFounderFront(@PathVariable int current, @PathVariable int limit) {

        Page<Founder> page = new Page<Founder>(current, limit);

        Map<String, Object> map = founderService.getFounderFront(page);

        return R.ok().data(map);
    }

    //  根据创始人id查询汽车
    @GetMapping("/getFounderFrontInfo/{id}")
    public R getFounderFrontInfo(@PathVariable int id) {

        Founder founder = founderService.getById(id);

        QueryWrapper<Automobile> wrapper = new QueryWrapper<>();
        wrapper.eq("founder_id", founder.getId());
        List<Automobile> list = automobileService.list(wrapper);

        return R.ok().data("founder", founder).data("automobile", list);
    }

    //    根据汽车id获取汽车的信息
    @PostMapping("/getAutomobileInfo/{id}")
    public AutomobileVo getAutomobileInfo(@PathVariable String id) {
        Automobile automobileId = automobileService.getById(id);
        AutomobileVo automobileVo = new AutomobileVo();
        BeanUtils.copyProperties(automobileId, automobileVo);
        return automobileVo;
    }


}
